/**
 * 
 */
package com.jm.web.form.table;


import java.io.IOException;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.AttributeBuilder;
import com.jm.utils.StringHelper;
import com.jm.web.form.support.FormItem;

/**
 * @author yanjie
 *
 * 2011/9/22 下午1:31:52
 */
public class Td extends FormItem{
	public final static String TAG_ITEMNAME = "TD";
	
	public  final static String TAG_COLSPAN = "COLSPAN";
	public  final static String TAG_ROWSPAN = "ROWSPAN";
	
	protected String tdBeginHtml 			= "";
	protected String tdBodyHtml 			= "";
	protected String tdEndHtml 				= "";
	
	public int colspan = 1;
	public int rowspan = 1;
	public int groupLev = -1;//分組的層級
	
	@Override
	public void OnSetProperty(String strName, String strValue) {
		if(TAG_COLSPAN.equalsIgnoreCase(strName)){
			this.colspan = StringHelper.GetValue(strValue, colspan) < 1 ? colspan :  StringHelper.GetValue(strValue, colspan) ;
		}
		
		if(TAG_ROWSPAN.equalsIgnoreCase(strName)){
			this.rowspan = StringHelper.GetValue(strValue, rowspan) < 1 ? rowspan :  StringHelper.GetValue(strValue, rowspan) ;
		}
		
		super.OnSetProperty(strName, strValue);
	}
	
	
	@Override
	public void FillAttributeBuilder(AttributeBuilder attributesBuilder) {
		if(this.getColspan() > 1)
			attributesBuilder.Set(TAG_COLSPAN.toLowerCase(), StringHelper.getValue(this.getColspan()));
		
		if(this.getRowspan() > 1)
			attributesBuilder.Set(TAG_ROWSPAN.toLowerCase(), StringHelper.getValue(this.getRowspan()));
		
		super.FillAttributeBuilder(attributesBuilder);
	}
	/**
	 * @param tag
	 */
	public Td() {}
	
	@Override
	public String getNodeName() {
		if(this.getParentItem() != null && this.getParentItem().getParentItem() != null && this.getParentItem().getParentItem() instanceof Thead)
			return "th";
		else
			return super.getNodeName();
	}
	
	@Override
	public void onOutItemBegin(JspWriter writer) {
		super.onOutItemBegin(writer);
	}
	
	@Override
	public void onOutItemEnd(JspWriter writer) {
		super.onOutItemEnd(writer);
	}
	
	
	public int getColspan() {
		return colspan;
	}

	public void setColspan(int colspan) {
		this.colspan = colspan;
	}

	public int getRowspan() {
		return rowspan;
	}

	public void setRowspan(int rowspan) {
		this.rowspan = rowspan;
	}
	
	public void outHtml(JspWriter out) {
		try {
			out.write(this.getTdBeginHtml());
			if(this.getRowspan() > 1){
				out.write(" rowspan = \""+this.getRowspan()+"\" ");
			}
			
			int mergeindex = this.GetExtValue("mergeindex",0);
			out.write(" mergeindex = \""+mergeindex+"\" ");
			
			out.write(" >");
			out.write(this.getTdBodyHtml());
			out.write(this.getTdEndHtml());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}


	public String getTdBeginHtml() {
		return tdBeginHtml;
	}


	public void setTdBeginHtml(String tdBeginHtml) {
		this.tdBeginHtml = tdBeginHtml;
	}


	public String getTdBodyHtml() {
		return tdBodyHtml;
	}


	public void setTdBodyHtml(String tdBodyHtml) {
		this.tdBodyHtml = tdBodyHtml;
	}


	public String getTdEndHtml() {
		return tdEndHtml;
	}


	public void setTdEndHtml(String tdEndHtml) {
		this.tdEndHtml = tdEndHtml;
	}


	public int getGroupLev() {
		return groupLev;
	}


	public void setGroupLev(int groupLev) {
		this.groupLev = groupLev;
	}
}
